const app = getApp()
const util = require('../../utils/util.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        canCommit1: false,
        canCommit2: false,
        canCommit3: false,
        canCommit4: false,
        canCommit5: false,
        deposit: '0.00'
    },

    /*
     *监听充值卡的输入
     */
    cardInput1: function (event) {
        var self = this;
        self.setData({
            canCommit1: this.inputLisener(event),
        });
    },
    cardInput2: function (event) {
        var self = this;
        self.setData({
            canCommit2: this.inputLisener(event),
        });
    },
    cardInput3: function (event) {
        var self = this;
        self.setData({
            canCommit3: this.inputLisener(event),
        });
    },
    cardInput4: function (event) {
        var self = this;
        self.setData({
            canCommit4: this.inputLisener(event),
        });
    },
    cardInput5: function (event) {
        var self = this;
        self.setData({
            canCommit5: this.inputLisener(event),
        });
    },
    /**
     * input 监听
     */
    inputLisener: function (event) {
        var self = this;
        if (event.detail.value.length > 0) {
            return true;
        } else {
            return false;
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //请求分享数据
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=wx_share&op=get_share',
            data: {},
            method: 'GET',
            dataType: 'json',
            success: res => {
                if (res.data.code == '200') {
                    this.setData({
                        share_default_title: res.data.datas.title,
                        share_default_image_url: res.data.datas.imageUrl
                    })
                }
            }
        })
        util.share();
        let self = this;
        wx.getStorage({
            key: 'key',
            success: (res) => {
                self.setData({
                    member_key: res.data
                });
                self.getBalance();
            }
        })
    },
    /**
     * 获取余额
     */
    getBalance: function () {
        var self = this;
        //获取余额
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_index&op=my_asset&key=' + self.data.member_key + '&fields=predepoit',
            success(res) {
                self.setData({
                    deposit: res.data.datas.predepoit
                });
            }
        });
    },
    /**
     * 确认提现
     */
    commitCard: function (event) {
        var self = this;
        wx.showLoading({
            title: "加载中",
        })
        let detail = event.detail.value;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=predeposit_cash&op=cash',
            method: "POST",
            data: {
                key: self.data.member_key,
                pdc_amount: detail.money,
                pdc_bank_name: detail.payee_bank,
                pdc_bank_no: detail.payee_account,
                pdc_bank_user: detail.payee_user,
                pay_password: detail.payee_password,
            },
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function (res) {
                if (res.data.code == 200) {
                    let pages = getCurrentPages();
                    let prevPage = pages[pages.length - 3];
                    prevPage.setData({
                        withdrawBack: true,
                    });
                    wx.navigateBack({
                        delta: 2
                    });
                    wx.showToast({
                        title: '您的提现申请已成功提交，请等待系统处理',
                        icon: 'none'
                    });
                } else {
                    wx.showToast({
                        title: res.data.datas.error,
                        icon: 'none'
                    })
                }
            },
            fail: function (res) {
                wx.showToast({
                    title: '网络请求失败',
                    icon: 'none'
                })
            }
        })
    },
    onShareAppMessage: function () {
        let share_key = this.data.member_key ? this.data.member_key : '';
        let share_path = '';
        if (util.getCurrentPageUrlWithArgs().indexOf("?") != -1) {
            share_path = util.getCurrentPageUrlWithArgs() + '&share_key=' + share_key;
        } else {
            share_path = util.getCurrentPageUrlWithArgs() + '?share_key=' + share_key;
        }
        return {
            title: this.data.share_default_title,
            imageUrl: this.data.share_default_image_url,
            path: share_path
        }
    }
})